// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var constant = require("constant");
	var css_pickup = require("./accountPrePickup.css");
	var databind = require("databind");
	var financeId;
	var financeState;
	var deliveryMethod;
	var quantities = [];
	var inventoryIds = [];
	var bulkstockIds = [];
	var units = [];
	var deliveryDate;
	var deliveryAddress;
	var financeNumber;
	var price;
	var caseQuantities = [];
	var remark;
	var tools = require("tools");

	var init = function(page) {
		financeId = page.params.financeId;
		financeNumber = page.params.financeNumber;
		inquireBulkstockInventoryPageList();
		inquireFinanceApplicationDetail();
		router.init();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}

		tools.inquireCommonSelectorList(9, $("#preDeliveryReason"));

		//点击项目基本详情financeDetail
		$(".financeDetail").unbind("click").click(function() {
			router.route("account/accountDetail.html?financeId=" + financeId, true);

		});

		$("#accountPrePickup").on("click", ".checkeds", function() {
			var price = 0;
			var value = 0;
			var interest = 0;
			var expense = 0;
			if ($("#preDeliveryDate").val() != "") {
				$.each($("#accountPrePickup .checkeds:checked"), function() {
					var interestEstimation = interestEstimationFinanceInventory($("#preDeliveryDate").val(), $(this).attr("inventoryId"), financeId);
					price += interestEstimation.summation * 1;
					value += interestEstimation.principal * 1;
					interest += interestEstimation.interest * 1;
					expense += interestEstimation.expense * 1;
				});
			}
			$("#accountPrePickup .estimateAmount").html(price.toFixed(2));
			$("#accountPrePickup .value").html(value.toFixed(2));
			$("#accountPrePickup .interest").html(interest.toFixed(2));
			$("#accountPrePickup .expense").html(expense.toFixed(2));
		});

		$("#preDeliveryDate").on("blur", function() {
			var price = 0;
			var value = 0;
			var interest = 0;
			var expense = 0;
			$.each($("#accountPrePickup .checkeds:checked"), function() {
				var _this = this;
				var interestEstimation = interestEstimationFinanceInventory($("#preDeliveryDate").val(), $(_this).attr("inventoryId"), financeId);
				price += interestEstimation.summation * 1;
				value += interestEstimation.principal * 1;
				interest += interestEstimation.interest * 1;
				expense += interestEstimation.expense * 1;
			});
			$("#accountPrePickup .estimateAmount").html(price.toFixed(2));
			$("#accountPrePickup .value").html(value.toFixed(2));
			$("#accountPrePickup .interest").html(interest.toFixed(2));
			$("#accountPrePickup .expense").html(expense.toFixed(2));
		});

		$("#prePickupSubmit").unbind("click").click(function() {
			inventoryIds = [];
			deliveryMethod = $("#accountPrePickup .deliveryMethod:checked").val();
			deliveryAddress = $("#preDeliveryAddress").val();
			deliveryDate = $("#preDeliveryDate").val();
			remark = $("#preRemark").val();

			for (var j = 0; j < $("#accountPrePickup .checkeds:checked").length; j++) {
				inventoryIds.push($($("#accountPrePickup .checkeds:checked")[j]).attr("inventoryId"));
			}
			if (deliveryMethod == null) {
				$("#accountPrePickup .error_info").html("请选择配送方式");
				$("#accountPrePickup .error_info").css("visibility", "visible");
			} else if (deliveryAddress == null || deliveryAddress == "") {
				$("#accountPrePickup .error_info").html("请输入配送地址");
				$("#accountPrePickup .error_info").css("visibility", "visible");
			} else if (deliveryDate == null || deliveryDate == "") {
				$("#accountPrePickup .error_info").html("请选择配送日期");
				$("#accountPrePickup .error_info").css("visibility", "visible");
			} else if (inventoryIds.length == 0) {
				$("#accountPrePickup .error_info").html("请选择货箱");
				$("#accountPrePickup .error_info").css("visibility", "visible");
			} else {
				$("#accountRansomPickup .error_info").css("visibility", "hidden");
				tools.protectDoubleClick();
				createPrePickupApplication();
			}

		});

	};

	//查询融资信息详情
	var inquireFinanceApplicationDetail = function() {
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var dm = data.datas[0].deliveryMethod;
					if (2 == dm || 3 == dm) {
						$("#accountPrePickup .deliveryMethod[value=2]").attr("checked", "checked");
					} else {
						$("#accountPrePickup .deliveryMethod[value=1]").attr("checked", "checked");
						$("#preDeliveryAddress").val($.trim(data.datas[0].receiveProvince) + $.trim(data.datas[0].receiveCity) + $.trim(data.datas[0].receiveDistrict) + $.trim(data.datas[0].receiveAddress));
					}
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};

	//查询融资箱货列表
	var inquireBulkstockInventoryPageList = function() {
		base.fnAjax({
			action: "inquireBulkstockInventoryPageList.json",
			async: true,
			oData: {
				financeId: financeId,
				inventoryState: 2
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulkstockPreInventoryTpl").html();
					var html = juicer(tpl, data);
					$(".accountPrePickup_preTableTitle").after(html);

					if ($("#preTableTitle").parent().find("tr").length == 1) {
						$("#accountPrePickup .pickupForm").html('<div class="emptyData">亲，您暂时还没有可申请提前还款的货物！</div>');
					}

					$.each($("#accountPrePickup tr"), function() {
						if ($.trim($(this).attr("pickupId")) != 0) {
							$(this).find("td").css("background-color", "#CCC");
							$(this).find("input").attr("disabled", "disabled");
						}
					});
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};

	//利息估算-指定货箱
	var interestEstimationFinanceInventory = function(cutoffDate, inventoryIds, financeId) {
		var interestEstimation = {
			summation: 0,
			interest: 0,
			principal: 0,
			expense: 0
		};
		base.fnAjax({
			action: "interestEstimationFinanceInventory1.json",
			async: false,
			oData: {
				cutoffDate: cutoffDate,
				inventoryId: inventoryIds,
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					//本息合计
					interestEstimation.summation = data.datas[0].summation;
					//利息
					interestEstimation.interest = data.datas[0].interest;
					//本金
					interestEstimation.principal = data.datas[0].principal;
					//其他费用
					interestEstimation.expense = data.datas[0].expense;
				} else {
					//					base.alert(2, "信息获取失败");
				}
			}
		});
		return interestEstimation;
	};

	//提前还款申请
	var createPrePickupApplication = function() {
		base.fnAjax({
			action: "createPrePickupApplication.json",
			async: false,
			oData: {
				inventoryIds: inventoryIds, //指定提货箱ID
				estimateAmount: $(".estimateAmount").html(), //预计还款
				deliveryAddress: deliveryAddress, //配送地址	
				deliveryDate: deliveryDate, //配送时间
				deliveryMethod: deliveryMethod, //配送方式 1:到港/2:到门
				pickupMethod: 1, //提货方式 1:现金提货/2:换货
				financeNumber: financeNumber, //融资信息编号
				financeId: financeId, //融资信息ID
				applicantReson: $("#preDeliveryReason").val(),
				remark: remark
			}, //传递参数
			fnSuccess: function(data) {
				tools.cancelProtectDoubleClick();
				if (data.status == 200) {
					router.route("account/accountDetail.html?financeId=" + financeId);
				} else {
					$(".error_info").html(data.message);
					$(".error_info").css("visibility", "visible");
				}
			}
		});
	};

	return {
		init: init
	};
});